package cn.com.qtgl.apis.iam.comp.service;

import cn.com.qtgl.apis.builtin.model.vo.Department;
import cn.com.qtgl.apis.iam.model.vo.DepartmentEdges;
import cn.com.qtgl.apis.iam.model.vo.IamApiResult;
import cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService;
import cn.melonlib.cores.graphql.comp.utils.GraphQLTools;
import cn.melonlib.cores.jpa.model.bean.impl.PaginationList;
import cn.melonlib.cores.model.anno.*;
import cn.melonlib.cores.model.input.OrderByInput;
import cn.melonlib.objects.maps.JsonMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

import static cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService.ROOT_METHOD.*;

@Service
@WorkerComment("部门信息")
@WorkerService(
        module = "apis",
        model = @WorkerModel(
                modelType = Department.class
                ,dependenceTypes = {UserService.class}
        )
        ,ignores = {LOAD_ALL,LIST,SAVE,REMOVE_BY_ID,REMOVE_BY_ID_LIST,TOTAL_COUNT,GET_BY_ID}
)
public class DepartmentService extends WorkerBasicService<Department> {

    @Resource
    private GraphQLTools graphQLTools;

    @Resource
    private IamCacheService iamCacheService;

    @Override
    @WorkerQuery
    @WorkerComment("查询岗位信息")
    public <DATA extends PaginationList<Department>> DATA search(
            @WorkerParameter(value = "first",comment = "数量",required = true) Integer first,
            @WorkerParameter(value = "offset",comment = "偏移量",required = true) Long offset,
            @WorkerParameter(value = "filter",comment = "过滤",required = true) JsonMap<JsonMap<Object>> filter,
            @WorkerParameter(value = "orderBy",comment = "排序") OrderByInput orderBy) throws Exception {
        IamApiResult<DepartmentEdges> departments=iamCacheService.departments();
        return (DATA) PaginationList.createOffset(departments.getEdges().stream().map(DepartmentEdges::getNode).filter(node-> graphQLTools.filter(node,filter)).skip(offset).limit(first)
                .collect(Collectors.toList()),0,departments.getEdges().size(),departments.getTotalCount());
    }
}
